<?php

namespace App\Repositories;

use App\Models\Tariff;
use BobCoder\Construct\Foundation\Repository\Repository;

class TariffRepository extends Repository
{
    protected $model;

    public function __construct()
    {
        $this->model = new Tariff();
    }


    /**
     * 资费列表
     * @param int $companyId 公司ID
     * @param string $type 类型
     * @param int $pageSize
     * @param array $columns
     * @return mixed
     * @Author 姿势就是力量
     */
    public function getPageList(int $companyId,int  $service_type, string $name,string $creator_name ,string $modifier_name,string $type,string $date,string $updated_at,int $pageSize = 10, array $columns = ['*'])
    {
        return $this->model
            ->select($columns)
            ->company($companyId)
            ->with(['company:id,name', 'service_type:id,name', 'creator:id,name', 'modifier:id,name'])
            ->when($name,function ($query)use($name){
                $query->where('name','like','%'.$name.'%');    //资费名称
            })
            ->when($service_type,function ($query) use($service_type){
                $query->where('service_type_id','like','%'.$service_type.'%');  //业务类型
            })
            ->when($creator_name,function ($query) use ($creator_name){
                $query->whereHas('creator',function ($query2)use ($creator_name){
                    $query2->where('name','like','%'.$creator_name.'%');     //创建人
                });
            })
            ->when($modifier_name,function ($query)use($modifier_name){
                $query->whereHas('modifier',function ($query2)use($modifier_name){
                   $query2->where('name','like','%'.$modifier_name.'%');       //修改人
                });
            })
            ->when($date,function ($query)use($date){
                if($date){
                    $list = explode(',',$date);
                    $query->whereBetween('created_at',[$list[0],$list[1]]); //时间选择
                }
            })
            ->when($updated_at,function ($query)use($updated_at){
                if($updated_at){
                    $list = explode(',',$updated_at);
                    $query->whereBetween('updated_at',[$list[0],$list[1]]); //时间选择
                }
            })
            ->where('type', $type)
            ->orderBy('created_at', 'desc')
            ->paginate($pageSize);
    }


    /**
     * 保存资费信息
     * @param array $data
     * @return Tariff|\Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Model
     * @Author 姿势就是力量
     */
    public function saveData(array $data)
    {
        if (!empty($data['id'])) {
            $model = $this->find($data['id']);
            $data['modifier_id'] = session('adminInfo')?session('adminInfo')['id']:0;
        } else {
            $data['creator_id'] = session('adminInfo')?session('adminInfo')['id']:0;
            $model = $this->model;
        }

        foreach ($data as $key => $value) {
            $model->{$key} = $value;
        }
        $model->save();

        return $model;
    }


    /**
     * 统计资费
     * @param array $condition
     * @return mixed
     * @Author 姿势就是力量
     */
    public function getCount(array $condition)
    {
        return $this->model->where($condition)->count();
    }

}
